// SnapEngine.cpp
//
#include "SnapEngine.h"
#include "MemCheck.h"

using namespace IDS::IDEAS::OpenGLView;

#if 0

// ************************************************************************************************
// ************************************************************************************************
// ************************************************************************************************
//			+---------+---------+
//			|         |         |
//			|    0    |    3    |
//			|         |         |
//			+---------+---------+
//			|         |         |
//			|    1    |    2    |
//			|         |         |
//			+---------+---------+
//int SnapEngine2D::SnapNode::getQuadrantOf(const IDSReal3D &q)
//{
//	//if( p.equals(q) )
//		return -1;
//
//	//if( q[0] < p[0] && q[1] > p[1]) return 0;
//	//if( q[0] < p[0] && q[1] < p[1]) return 1;
//	//if( q[0] > p[0] && q[1] < p[1]) return 2;
//	//if( q[0] > p[0] && q[1] > p[1]) return 3;
//}

// ************************************************************************************************
// ************************************************************************************************
// ************************************************************************************************


// ------------------------------------------------------------------------------------------------
SnapEngine2D::SnapEngine2D()
{
	//root = NULL;
	reset();
}

// ------------------------------------------------------------------------------------------------
SnapEngine2D::~SnapEngine2D()
{
	reset();
}

// ------------------------------------------------------------------------------------------------
void SnapEngine2D::insertPoint(const IDSReal3D &chart, const IDSReal3D &geo)
{
	points.push_back( std::make_pair(chart,geo) );
}

// ------------------------------------------------------------------------------------------------
void SnapEngine2D::reset()
{
	points.clear();
}

// ------------------------------------------------------------------------------------------------
void SnapEngine2D::removePoint(const IDSReal3D &chart)
{
	bool found = false;
	for(std::list<std::pair<IDSReal3D,IDSReal3D> >::iterator it = points.begin();
		it != points.end();
		/**/)
	{
		std::list<std::pair<IDSReal3D,IDSReal3D> >::iterator jt = it++;
		if( jt->first.equals(chart,IDSMath::TOLERANCE) )
		{
			points.erase(jt);
			break;
		}
	}
}

// ------------------------------------------------------------------------------------------------
bool SnapEngine2D::findPoint(const IDSReal3D &q, IDSReal3D &targetChart,IDSReal3D &targetGeo, double accuracy)
{
	bool found = false;
	double sqrDst = accuracy * accuracy * 2;

	for(std::list< std::pair<IDSReal3D,IDSReal3D> >::iterator it = points.begin();
		it != points.end();
		++it)
	{
		if( it->first.equals(q,accuracy) )
		{
			double d = (it->first-q).sqrmod();
			if( d < sqrDst)
			{
				sqrDst = d;

				found = true;
				targetChart = it->first;
				targetGeo   = it->second;
			}
			//break;
		}
	}

	return found;
}
#endif